import { x6Service } from '@/services/test-services';
import type { X6API } from '@/models/models';
import type { Effect, Reducer, Subscription } from '@umijs/max';
const { queryX6DataList } = x6Service;

export interface IndexModelType {
  namespace: 'x6Model';
  state: X6API.IState;
  effects: {
    queryX6DataList: Effect;
  };
  reducers: {
    save: Reducer<X6API.IState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'x6Model',

  state: {
    name: '',
    mobileDataList: [],
  },

  effects: {
    *queryX6DataList({}, { call, put }): any {
      const response = yield call(queryX6DataList, {});
      console.log('userList', response);
      try {
        if (response.success) {
          yield put({
            type: 'save',
            payload: {
              mobileDataList: response.data,
            },
          });
        }
      } catch (error) {
        console.error(error);
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen((listener) => {
        const { location } = listener;
        if (location.pathname.includes('/X6Cases')) {
          dispatch({
            type: 'queryX6DataList',
          });
        }
      });
    },
  },
};

export default IndexModel;
